<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="20" :xs="24">

        <!-- 新增、修改 btn -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col> 
        </el-row>

        <!-- table列表 -->
        <el-table
          v-loading="loading"
          :data="list"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column label="序号" type="index" width="50" />
          <el-table-column
            label="平台名称"
            align="center"
            prop="platformName"
            width="120"
          />
          <el-table-column
            label="司机数量"
            align="center"
            prop="driverCount"
            width="120"
          />
          <el-table-column
            label="平均评分"
            align="center"
            prop="averageRating"
            width="120"
          />
          <el-table-column
            label="违规次数"
            align="center"
            prop="violationCount"
            width="120"
          />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.current"
          :limit.sync="queryParams.size"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="平台">
              <el-select v-model="form.platformId" placeholder="请选择平台">
                <el-option
                  v-for="item in platformList"
                  :key="item.platformId"
                  :label="item.platformName"
                  :value="item.platformId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机数量" prop="driverCount">
              <el-input
                v-model="form.driverCount"
                placeholder="请输入司机数量"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平均评分" prop="averageRating">
              <el-input
                v-model="form.averageRating"
                placeholder="请输入平均评分"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="违规次数" prop="violationCount">
              <el-input
                v-model="form.violationCount"
                placeholder="请输入违规次数"
                maxlength="30"
              />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, save, submit , detail} from "@/api/driver";
import { simpleList } from "@/api/platform";
export default {
  name: "Driver",
  // 组件
  components: {},
  // 数据
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 用户表格数据
      list: [],
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
      },
      // 下拉框
      platformList: [],
      // 表单参数
      form: {},
      // 是否显示弹出层
      open: false,
    
    };
  },
  // 监听
  watch: {},
  // 构造函数
  created() {
    this.getList();
  },
  // 函数方法
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        // 后端返回
        console.log("userList", response);

        const data = response.data;
        this.list = data.records;
        this.total = data.total;
        this.loading = false;
      });
    },
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + userIds + '"的数据项？')
        .then(function () {
          const data = {
            ids: userIds,
          };
          return remove(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 新增
    handleAdd() {
      this.reset();
      simpleList().then((res) => {
        this.platformList = res.data;
        this.open = true;
        this.title = "添加";
      });
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      console.log('handleUpdate', row)
      simpleList().then((res) => {
        this.platformList = res.data;
      });
      detail(row.id).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    submitForm: function () {
      console.log("submitForm", this.form);
      if (this.form.id != undefined) {
        // 修改
        submit(this.form).then((res) => {
          if (res.success) {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          } else {
            this.$modal.msgWarning(res.msg);
          }
        });
      } else {
        // 新增
        save(this.form).then((res) => {
          if (res.success) {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          } else {
            this.$modal.msgWarning(res.msg);
          }
        });
      }
    },

  },
};
</script>

<style scoped lang="scss"></style>
